package com.ttn.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttn.common.core.entity.Result;
import com.ttn.common.core.entity.studentrecord.LearningProgressRecord;
import com.ttn.common.core.entity.studentrecord.TestStudyRecord;
import com.ttn.common.core.entity.studentrecord.bo.TestStudyRecordBO;
import com.ttn.common.core.entity.studentrecord.query.UnitReportQuery;
import com.ttn.common.core.entity.studentrecord.vo.TestStudyRecordVO;
import com.ttn.student.mapper.TestStudyRecordMapper;
import com.ttn.student.openfeign.BusinessFeignService;
import com.ttn.student.service.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

@Service
public class TestStudyRecordServiceImpl extends ServiceImpl<TestStudyRecordMapper, TestStudyRecord> implements TestStudyRecordService {

    @Resource
    private LearningProgressRecordService learningProgressRecordService;

    @Resource
    private TestStudyRecordMapper testStudyRecordMapper;
    @Resource
    private VocabularyStudyRecordDetailsService vocabularyStudyRecordDetailsService;
    @Resource
    private GrammarTestStudyRecordDetailsService grammarTestStudyRecordDetailsService;
    @Resource
    private HearingDetailsStudyRecordDetailsService hearingDetailsStudyRecordDetailsService;
    @Resource
    private OralPracticeStudyRecordDetailsService oralPracticeStudyRecordDetailsService;
    @Resource
    private OralConversationStudyRecordDetailsService oralConversationStudyRecordDetailsService;
    @Resource
    private BusinessFeignService businessFeignService;


    @Transactional
    @Override
    public Result saveTestStudyRecord(TestStudyRecordBO testStudyRecordBO) {
        TestStudyRecord testStudyRecord = testStudyRecordBO.getTestStudyRecord();
        TestStudyRecordVO testStudyRecordVO = new TestStudyRecordVO();
        if (testStudyRecordBO.getRestart().equals(0)) {
            LambdaQueryWrapper<TestStudyRecord> testStudyRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
            testStudyRecordLambdaQueryWrapper.eq(TestStudyRecord::getFinishFlag, 0);
            testStudyRecordLambdaQueryWrapper.eq(TestStudyRecord::getStudentId, testStudyRecordBO.getTestStudyRecord().getStudentId());
            testStudyRecordLambdaQueryWrapper.eq(TestStudyRecord::getCourseUnitId, testStudyRecordBO.getTestStudyRecord().getCourseUnitId());
            testStudyRecordLambdaQueryWrapper.eq(TestStudyRecord::getUnitTaskType, testStudyRecordBO.getTestStudyRecord().getUnitTaskType());
            testStudyRecordLambdaQueryWrapper.orderByDesc(TestStudyRecord::getCreateTime);
            testStudyRecordLambdaQueryWrapper.last(" limit 1");
            TestStudyRecord queryTestStudyRecord = this.getOne(testStudyRecordLambdaQueryWrapper);
            if (Objects.isNull(queryTestStudyRecord)) {
                testStudyRecord.setScore(0);
                testStudyRecord.setBeyondProportion(BigDecimal.ZERO);
                this.save(testStudyRecord);
            } else {
                testStudyRecord = queryTestStudyRecord;
            }

        } else {
            //重新开始
            testStudyRecord.setScore(0);
            testStudyRecord.setBeyondProportion(BigDecimal.ZERO);
            this.save(testStudyRecord);
        }
        testStudyRecordVO.setTestStudyRecord(testStudyRecord);
        //单元任务(1词汇2听力3语法4口语跟读5口语会话)
        if (testStudyRecordBO.getTestStudyRecord().getUnitTaskType().equals("1")) {
            //获取题目数
            Result<Long> countCourseUnitNumResult = businessFeignService.serviceQueryCourseUnitVocabularyByCourseUnitId(testStudyRecord.getCourseUnitId());
            if (countCourseUnitNumResult.isSuccess() && countCourseUnitNumResult.getData() > 0) {
                //更新题目数据
                testStudyRecord.setCourseUnitNum(countCourseUnitNumResult.getData().intValue());
            }
            //词汇
            testStudyRecordVO.setCourseUnitVocabularyList(vocabularyStudyRecordDetailsService.getUnfinishedCourseUnitVocabularyList(testStudyRecord));
        } else if (testStudyRecordBO.getTestStudyRecord().getUnitTaskType().equals("2")) {
            //2听力
            Result<Long> countCourseUnitNumResult = businessFeignService.serviceQueryCourseCourseUnitHearingDetailsByCourseUnitId(testStudyRecord.getCourseUnitId());
            if (countCourseUnitNumResult.isSuccess() && countCourseUnitNumResult.getData() > 0) {
                //更新题目数据
                testStudyRecord.setCourseUnitNum(countCourseUnitNumResult.getData().intValue());
            }

            testStudyRecordVO.setCourseUnitHearingList(hearingDetailsStudyRecordDetailsService.getUnfinishedCourseUnitHearingDetailsList(testStudyRecord));

        } else if (testStudyRecordBO.getTestStudyRecord().getUnitTaskType().equals("3")) {
            //3语法
            Result<Long> countCourseUnitNumResult = businessFeignService.serviceQueryCourseUnitGrammarTestByCourseUnitId(testStudyRecord.getCourseUnitId());
            if (countCourseUnitNumResult.isSuccess() && countCourseUnitNumResult.getData() > 0) {
                //更新题目数据
                testStudyRecord.setCourseUnitNum(countCourseUnitNumResult.getData().intValue());
            }
            testStudyRecordVO.setCourseUnitGrammarTestList(grammarTestStudyRecordDetailsService.getUnfinishedCourseUnitGrammarTestList(testStudyRecord));

        } else if (testStudyRecordBO.getTestStudyRecord().getUnitTaskType().equals("4")) {
            //4口语跟读
            Result<Long> countCourseUnitNumResult = businessFeignService.serviceQueryCourseUnitOralPracticeByCourseUnitId(testStudyRecord.getCourseUnitId());
            if (countCourseUnitNumResult.isSuccess() && countCourseUnitNumResult.getData() > 0) {
                //更新题目数据
                testStudyRecord.setCourseUnitNum(countCourseUnitNumResult.getData().intValue());
            }
            testStudyRecordVO.setCourseUnitOralPracticeList(oralPracticeStudyRecordDetailsService.getUnfinishedCourseUnitOralPracticeList(testStudyRecord));

        } else if (testStudyRecordBO.getTestStudyRecord().getUnitTaskType().equals("5")) {
            //5口语会话
            Result<Long> countCourseUnitNumResult = businessFeignService.serviceQueryCourseUnitOralConversationByCourseUnitId(testStudyRecord.getCourseUnitId());
            if (countCourseUnitNumResult.isSuccess() && countCourseUnitNumResult.getData() > 0) {
                //更新题目数据
                testStudyRecord.setCourseUnitNum(countCourseUnitNumResult.getData().intValue());
            }
            testStudyRecordVO.setCourseUnitOralConversationList(oralConversationStudyRecordDetailsService.getUnfinishedCourseUnitOralConversationList(testStudyRecord));

        }
        this.updateById(testStudyRecord);

        LambdaQueryWrapper<LearningProgressRecord> progressRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        progressRecordLambdaQueryWrapper.eq(LearningProgressRecord::getGrade, testStudyRecordBO.getTestStudyRecord().getGrade());
        progressRecordLambdaQueryWrapper.eq(LearningProgressRecord::getCourseUnitId, testStudyRecordBO.getTestStudyRecord().getCourseUnitId());
        progressRecordLambdaQueryWrapper.eq(LearningProgressRecord::getStudentId, testStudyRecordBO.getTestStudyRecord().getStudentId());

        if (learningProgressRecordService.count(progressRecordLambdaQueryWrapper) == 0) {
            LearningProgressRecord learningProgressRecord = new LearningProgressRecord();
            learningProgressRecord.setGrade(testStudyRecordBO.getTestStudyRecord().getGrade());
            learningProgressRecord.setCourseUnitId(testStudyRecordBO.getTestStudyRecord().getCourseUnitId());
            learningProgressRecord.setStudentId(testStudyRecordBO.getTestStudyRecord().getStudentId());
            learningProgressRecord.setCourseUnitNum(businessFeignService.serviceQueryQuestionsNumberByUnitId(testStudyRecordBO.getTestStudyRecord().getCourseUnitId()).getData());
            learningProgressRecordService.save(learningProgressRecord);
        }
        return Result.success(testStudyRecordVO);
    }

    @Override
    public List<TestStudyRecord> queryTestStudyRecordByUnitId(Integer unitId, Integer studentId) {
        return testStudyRecordMapper.queryTestStudyRecordByUnitId(unitId, studentId);
    }

    @Override
    public int queryTotalTestStudyRecordByUnitIdId(Integer unitId, Integer studentId) {
        return testStudyRecordMapper.queryTotalTestStudyRecordByUnitIdId(unitId, studentId);
    }

    @Override
    public int queryTotalCorrectAmountByUnitIdId(Integer unitId, Integer studentId) {
        return testStudyRecordMapper.queryTotalCorrectAmountByUnitIdId(unitId, studentId);
    }

    @Override
    public List<TestStudyRecord> queryUnitReport(UnitReportQuery unitReportQuery) {
        return testStudyRecordMapper.queryUnitReport(unitReportQuery);
    }

    @Override
    public int querySumCourseUnitNumber(Integer grade, Integer studentId) {
        return testStudyRecordMapper.querySumCourseUnitNumber(grade, studentId);
    }


}
